//
//  FAI.m
//  ui
//
//  Created by 袁飞 on 12-7-19.
//
//

#import "FAI.h"
#import "FObject.h"
#import "FUtil.h"
/*
moveto 150,50 2;
eat;
moveto 300,50 2;
turn;
moveto 150,50 2;
eat;
moveto 50,50 2;
turn;
*/

@implementation FAI
-(void)start{
  
}
-(void)onMoveEnd{
  
}
-(void)onActionEnd:(id)sender Data:(NSString*)data{
  
}
@end

@implementation FSeqAI
-(id)initWithFObject:(FObject*)obj{
  [super init];
  object_ = obj;
  index_ = 0;
  
  //actions_ = [NSMutableArray array];
  const char *td =
  "moveto 200,50 4;"\
  "eat;"\
  "moveto 400,50 4;"\
  "turn;"\
  "moveto 200,50 4;"\
  "eat;"\
  "moveto 100,50 4;"\
  "turn;";
  NSString* data = [NSString stringWithUTF8String:td];
  actions_ = [FUtil splitStr:data byFlag:@";"];
  [actions_ retain];
  //[obj moveTo:ccp(150,50) Dur:2.0 CallTarget:@selector(onMoveEnd:)];
  return self;
}

-(void)performAction:(NSString*)cmd{
  NSMutableArray *args = [FUtil splitStr:cmd byFlag:@" "];
  NSString* t1 = [args objectAtIndex:0];
  //NSString* t2 = [args objectAtIndex:1];
  //NSString* t3 = [args objectAtIndex:2];

  NSString* cmdtype = [args objectAtIndex:0];
  if ([cmdtype compare:@"moveto"] == NSOrderedSame) {
    CGPoint p = [FUtil string2Point:[args objectAtIndex:1]];
    float dur = [[args objectAtIndex:2] floatValue];
    [object_ moveTo:p Dur:dur];
    [object_ runActionRepeated:@"move" WithSprite:@"test"];
  }else if([cmdtype compare:@"eat"] == NSOrderedSame){
    [object_ runAction:cmdtype WithSprite:@"test" Dur:1.0];
  }else if([cmdtype compare:@"turn"] == NSOrderedSame){
    [object_ runAction:cmdtype WithSprite:@"test" Dur:1.0];
  }
}

-(void)start{
  NSString* cmd = [actions_ objectAtIndex:index_];
  [self performAction:cmd];
  //[object_ moveTo:ccp(150,50) Dur:2.0];
  //[object_ runActionRepeated:@"move" WithSprite:@"test"];
}

-(void)onMoveEnd{
  [object_ stopAllAction:@"test"];
  index_ ++;
  NSString* cmd = [actions_ objectAtIndex:(index_%actions_.count)];
  [self performAction:cmd];
  /*[object_ stopAllAction:@"test"];
  CGPoint p = [object_ position];
  if (p.x == 150) {
    [object_ runAction:@"eat" WithSprite:@"test" Dur:1.0];
    //[o2 runAction:@"eat" WithSprite:@"test" Dur:1.0 CallTarget:ai Sel:@selector(onActionEnd:Data:)];
  }else if(p.x == 300){
    [object_ runAction:@"turn" WithSprite:@"test" Dur:1.0];
  }*/
}

-(void)onActionEnd:(id)sender Data:(NSString*)data{
  if([data compare:@"turn"] == NSOrderedSame){
    CCSprite* sp = [object_ getSpriteByName:@"test"];
    bool isflip = sp.flipX;
    [sp setFlipX:!isflip];
  }
  index_ ++;
  NSString* cmd = [actions_ objectAtIndex:(index_%actions_.count)];
  [self performAction:cmd];
  //NSString* d = [NSString stringWithUTF8String:data];
  /*NSLog(@"%@", data);
  if([data compare:@"eat"] == NSOrderedSame){
    [object_ moveTo:ccp(300,50) Dur:2.0];
    [object_ runActionRepeated:@"move" WithSprite:@"test"];
  }else if([data compare:@"turn"] == NSOrderedSame){
    CCSprite *sp = [object_ getSpriteByName:@"test"];
    [sp setFlipX:true];
    [object_ moveTo:ccp(150,50) Dur:2.0];
    [object_ runActionRepeated:@"move" WithSprite:@"test"];
  }*/
}

@end




@implementation FRandomAI
-(id)initWithFObject:(FObject*)obj{
  [super init];
  object_ = obj;
  return self;
}

-(void)onMoveEnd{
  [object_ stopAllAction:@"test"];
  CGPoint begin = [object_ position];
  
  float x = rand()%(460-120)+120;

  CCSprite* sp = [object_ getSpriteByName:@"test"];
  bool isflip = sp.flipX;
  //int direction = rand()%2;
  
  if (x < begin.x && !isflip) {
    [object_ runAction:@"turn" WithSprite:@"test" Dur:1.0];
  }else if (x > begin.x && isflip) {
    [object_ runAction:@"turn" WithSprite:@"test" Dur:1.0];
  }else{
    [self randomMove];
  }
  
  //float x = rand()%(460-120)+120;
  //float y = rand()%(300-20)+20;
  //CGPoint end = ccp(x, y);
  
  
}

-(void)onActionEnd:(id)sender Data:(NSString*)data{
  if([data compare:@"turn"] == NSOrderedSame){
    CCSprite* sp = [object_ getSpriteByName:@"test"];
    bool isflip = sp.flipX;
    [sp setFlipX:!isflip];
    [self randomMove];
  }
}

-(void)start{
  float x = rand()%(460-120)+120;
  float y = rand()%(300-20)+20;
  [object_ setPosition:ccp(x,y)];
  
  int direction = rand()%2;
  if (direction == 1) {
   bool flip = [[object_ getSpriteByName:@"test"] flipX];
   [[object_ getSpriteByName:@"test"] setFlipX:!flip];
  }
  [self randomMove];
}

-(void)randomMove{
  CGPoint begin = [object_ position];
  CCSprite* sp = [object_ getSpriteByName:@"test"];
  bool isflip = sp.flipX;

  float x = rand()%(460-120)+120;
  float y = rand()%(300-20)+20;
  if (isflip) {
    x = rand()%((int)begin.x-120) + 120;
    //y = rand()%60 + begin.y - 30;
    y = rand()%(300-20)+20;
  }else{
    x = rand()%(460 - (int)begin.x) + begin.x;
    //y = rand()%60 + begin.y - 30;
    y = rand()%(300-20)+20;
  }
  //float x = rand()%(460-120)+120;
  //float y = rand()%(300-20)+20;
  //for (int i=0; i<100; i++) {
    
  //}
  CGPoint end = ccp(x, y);
  float speed = rand()%100;
  float dur = ccpDistance(begin, end)/speed;
  
  [object_ moveTo:end Dur:dur];
  [object_ runActionRepeated:@"move" WithSprite:@"test"];
}

@end
